package abd.pr2.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;




import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import abd.pr2.bd.DAO;
import abd.pr2.model.Noticia;

public class NoticiasWindow extends JFrame implements ActionListener,ListSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox<String> etiquetaCombo;
	private JList<Noticia> noticiasList;
	private VisorNoticias contenido;
	private DAO dao;
	public NoticiasWindow(DAO dao){
		super();
		this.dao=dao;
		this.build();
		this.setNoticiaCombo(dao.consultarEtiquetas());
		this.setSize(500, 500);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setEnabled(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Visor de Noticias");
	}

	
	private void build(){
		this.getContentPane().setLayout(new BorderLayout());
		JPanel categoriasPanel = new JPanel(new BorderLayout());
		JLabel categoriaLabel= new JLabel("Categoría:");
		etiquetaCombo=new JComboBox<String>();
		etiquetaCombo.addActionListener(this);
		categoriasPanel.add(categoriaLabel,BorderLayout.WEST);
		categoriasPanel.add(etiquetaCombo,BorderLayout.CENTER);
		JPanel listaContainer = new JPanel(new BorderLayout());
		noticiasList=new JList<Noticia>();
		noticiasList.addListSelectionListener(this);
		listaContainer.add(categoriasPanel,BorderLayout.NORTH);
		listaContainer.add(noticiasList,BorderLayout.CENTER);
		contenido=new VisorNoticias();
		this.getContentPane().add(listaContainer,BorderLayout.NORTH);
		this.getContentPane().add(new JScrollPane(contenido),BorderLayout.CENTER);
	}
public void setNoticiaCombo(List<String> etiquetas){
		
		this.etiquetaCombo.removeAllItems();
		
		for(int i=0;i<etiquetas.size();i++){
			this.etiquetaCombo.addItem(etiquetas.get(i));
		}
	}
	public void setNoticiaList(List<Noticia> noticias){
		noticiasList.setListData(noticias.toArray(new Noticia[noticias.size()]));
	}
	public void setContenido(String contenido){
		this.contenido.setText(contenido);
	}
	public String getEtiquetaCombo(){
		return  (String) this.etiquetaCombo.getSelectedItem();
	}
	public Noticia getSelectedNoticia(){
		return noticiasList.getSelectedValue();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		setNoticiaList(this.dao.consultarNoticiasPorEtiqueta(this.getEtiquetaCombo()));
		
	}
	@Override
	public void valueChanged(ListSelectionEvent e) {
		// TODO Auto-generated method stub
		if(getSelectedNoticia()!=null && !e.getValueIsAdjusting()){
			this.contenido.setBodyText(this.dao.consultarInformacionNoticia(getSelectedNoticia().getId()));
		}		
	}
}
